package com.itHaipeng.service.impl;

import com.itHaipeng.mapper.BrandMapper;
import com.itHaipeng.pojo.Brand;
import com.itHaipeng.service.BrandService;
import com.itHaipeng.util.PageBean;
import com.itHaipeng.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
    //创建sqlSessionFactory
    private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public List<Brand> selectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }

    @Override
    public void insert(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.insert(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteById(int[] ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteById(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int page, int size) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算begin 的值
        int begin=(page-1)*size;
        List<Brand> brands = mapper.selectByPage(begin, size);
        int i = mapper.selectTotalCount();
        //存入pageBean中
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setTotalCount(i);
        pageBean.setRows(brands);
        sqlSession.close();
        System.err.println(pageBean.toString());
        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageCondition(int page, int size, Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算begin 的值
        int begin=(page-1)*size;
        //模糊查询
        String brandName = brand.getBrandName();
        String companyName = brand.getCompanyName();
        if(brandName!=null&&brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
       if(companyName!=null&&companyName.length()>0){
           brand.setCompanyName("%"+companyName+"%");
       }
        List<Brand> brands = mapper.selectByPageCondition(begin, size, brand);
        int i = mapper.selectTotalCountCondition(brand);
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setRows(brands);
        pageBean.setTotalCount(i);
        return pageBean;
    }
}
